<?php

 require_once(PATCH_DRIVERS.'drv.func.php');
 require_once(PATCH_DRIVERS.'ginst.func.php');
 require_once(PATCH_DRIVERS.'new_remote.func.php');

 class __kernel_init {
 
	static $jscript = array();
    static $modules;
	
	function start() {

		global $config;
		global $init;
		
		# Initilizing install modules		
			if(!$_SESSION['init']['modules'] || $config['site']['underReconstruction'] == true) {
				self::$modules = $this->getModulesList($_SESSION['user']['access']);
                
				if($config['site']['underReconstruction'] == false && isset($config['site']['underReconstruction']))
					$_SESSION['init']['modules'] = self::$modules;
			} else
				self::$modules = $_SESSION['init']['modules'];
			
		
		# Path
			$module = $_GET[0];
			if($_GET[1] != 'default' && $_GET[1] != '*') $page   = $_GET[1];
			else $page = "";

		# Initilizing and run module
			# User run module
				if(!empty($module)) {
					if(is_array(self::$modules)) {
						foreach(self::$modules as $module_name => $module_array) {
							if($module_name==$module) {
								$ini   = "module";
								$title = $module_array['module_title'];
							}
						}
					}
					
					if(!$ini && $GLOBALS['subdomain'] == false) {
						$ini    = "kernel";
						$module = "error";
						$page   = "404";
					}
					
				} else {
					
					$ini    = "module";
					$module = $_GET['m'] = $config['site']['default_module'];
					
					$module_secret = true;
					
					if(isset($config['site']['default_module_page'])) {
						if(is_array($config['site']['default_module_page'])) {
							foreach($config['site']['default_module_page'] as $getKey=>$getValue) {
								$_GET[$getKey] = $getValue;
							}
							
							if(isset($config['site']['default_module_page'][1])) $page = $config['site']['default_module_page'][1];
						} else {
							$page   = $config['site']['default_module_page'];
						}
						
					}
				}
				


			__kernel_start::onlyStartModule("user");
				
		    # Run autorun classes before page
		        if(is_array($config['autorun']['before'])) {
				        foreach($config['autorun']['before'] as $Aclassname) {
					        $init['autorun']["__".$Aclassname['where']."_".$Aclassname['class']] = &__lib_start::$Aclassname['where']($Aclassname['class'], 'autorun', '', '', true);
				        }
				}
				
			$page_print = &__kernel_start::$ini($module, $page, $title, array(), $module_secret);
			
			# Run autorun classes after page
			    if(is_array($config['autorun']['after'])) {
				        foreach($config['autorun']['after'] as $Aclassname) {
					        $init['autorun']["__".$Aclassname['where']."_".$Aclassname['class']] = &__lib_start::$Aclassname['where']($Aclassname['class'], 'autorun');
				        }
				}
								
			
				
			$this->display['center'] = __driver_display::si()->fetch("block-center.html", __driver_display::si()->assign("block", array(
					"title"	=> $init['start']['__'.$ini.'_'.$module]->title,
					"body"	=> $page_print
				)));
				
			
	}
	
	function getModulesList($acsgp = 1) {

		if($GLOBALS['config']['to_install'] == true) return array('admin' => array());
		
		$where[] = "`active` = 1";
		
		if(is_array(drv(ACSGP)->getAccesses()))
			foreach(drv(ACSGP)->getAccesses() as $access=>$true) {
				$whereOR[] = "`modules`.`access` = $access";
			}
		
		# create array modules list for this user
			$res = __driver_db::si()->query("
				SELECT
				 `modules`.`id`          AS `module_id`,
				 `modules`.`module_name` AS `module_name`,
				 `modules`.`access`      AS `module_access`
				 
				FROM `modules`
				WHERE ".(is_array($whereOR)?'('.implode(' OR ', $whereOR).') AND ':'').(is_array($where)?implode(' AND ', $where):''));

			while($row=&$res->fetchRow(DB_FETCHMODE_ASSOC)) {
				$modules[$row['module_name']] = $row;
			}
			
			return $modules;
	}
	
 }
 

//if(INIT::si("initilize") != true) trigger_error('INIT::si("initilize") return false :(', E_USER_ERROR);

?>
